<?php

namespace app\system\controller;

use app\common\model\logic\OrderPayModel;
use think\Request;

class OrderPayController extends BaseController
{
    private $orderPayModel;
    /**
     * 初始化
     * AreaController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->orderPayModel = new OrderPayModel();
    }

    public function index(){
        return $this->fetch();
    }
    //分页
    public function pagelist(){
        $pageSize = $this->request->param('pageSize/d');
        $pageNumber = $this->request->param('pageNumber/d');
        //排序
        $sortName = $this->request->param('sortName/s');
        $sortOrder = $this->request->param('sortOrder/s');
        if(!empty($sortName) && !empty($sortOrder)){
            $order="{$sortName} {$sortOrder}";
        }else{
            $order="sort desc";
        }
        $orderTitle = $this->request->param('order_title');
        if(!empty($orderTitle)) $map['order_title'] = ['like','%'.$orderTitle.'%'];
        $actionName = $this->request->param('action_name');
        if(!empty($actionName)) $map['action_name'] = ['like','%'.$actionName.'%'];
        $carSn = $this->request->param('car_sn');
        if(!empty($carSn)) $map['car_sn'] = ['like','%'.$carSn.'%'];
        $rangTime = $this->request->param('rang_time');
        if(!empty($rangTime)){
            $rangTime = explode('~',$rangTime);
            $map['pay_time'] = ['between',[strtotime($rangTime[0].' 0:00:00'),strtotime($rangTime[1].' 0:00:00')]];
        }
        try{
            $total = $this->orderPayModel->where($map)->count(); //检索过后的条数
            if($total==0){
                $data['total'] = 0;
                $data['rows'] = [];
                $data['sql'] = $this->orderPayModel->getLastSql();
                return $data;
            }
            $list = $this->orderPayModel->field(true)->where($map)->order($order)->page($pageNumber,$pageSize)->select();
            foreach($list as $k=>$v){
                $list[$k]['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
            }
            $static = $this->orderPayModel
                ->field("sum(pay_money) as total_pay_money,sum(money_bala) as total_money_bala,sum(money_discount) as total_money_discount")
                ->where($map)->find();
            $data['total'] = $total;
            $data['rows'] = $list;
            $data['static'] = $static;
            //$data['sql'] = $this->orderPayModel->getLastSql();
            return $data;
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
    }
}